package com.hexinfo.dmpro.sparing.model;

import cn.hutool.core.io.file.FileAppender;
import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import io.swagger.annotations.ApiModel;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.util.Date;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;

import lombok.experimental.Accessors;
import org.springframework.validation.annotation.Validated;

/**
 * 热备表级同步记录表
 *
 * @author yemw
 * @date 2024-04-28 14:19:29
 */
@Data
@Validated
@TableName("TD_HOT_STANDBY_TABLE")
@EqualsAndHashCode(callSuper = true)
@ApiModel(value = "热备表级同步记录表")
@Accessors(chain = true)
public class HotStandbyTable extends Model<HotStandbyTable> {
private static final long serialVersionUID = 1L;

    /**
     * id
     */
    @TableId(type= IdType.ASSIGN_UUID)
    @ApiModelProperty(value="id")
    private String id;
    /**
     * 任务日期
     */
    @ApiModelProperty(value="任务日期")
    private String taskDate;
    /**
     * 源名
     */
    @ApiModelProperty(value="源名")
    private String sourceName;
    /**
     * 库名
     */
    @ApiModelProperty(value="库名")
    private String dbName;
    /**
     * 表名
     */
    @ApiModelProperty(value="表名")
    private String tbName;
    /**
     * 库表名
     */
    @ApiModelProperty(value="库表名")
    private String dbTbName;
    /**
     * HDFS数量
     */
    @ApiModelProperty(value="HDFS数量")
    private String hdfsNumber;
    /**
     * 状态(同步失败、刷新失败、删除失败、比对失败、已完成、进行中、待执行)
     */
    @ApiModelProperty(value="状态(同步失败、刷新失败、删除失败、比对失败、已完成、进行中、待执行)")
    private String taskState;
    /**
     * 热备用时
     */
    @ApiModelProperty(value="热备用时")
    private String hotStandbyTime;
    /**
     * 用时详细
     */
    @ApiModelProperty(value="用时详细")
    private String hotStandbyTimeData;
    /**
     * 日志路径
     */
    @ApiModelProperty(value="日志路径")
    private String logPath;
    /**
     * 同步范围结束时间
     */
    @ApiModelProperty(value="同步范围结束时间")
    private String syncRangeEndTime;
    /**
     * 同步范围开始时间
     */
    @ApiModelProperty(value="同步范围开始时间")
    private String syncRangeStartTime;
    /**
     * 开始时间
     */
    @ApiModelProperty(value="开始时间")
    private String startTime;
    /**
     * 结束时间
     */
    @ApiModelProperty(value="结束时间")
    private String endTime;
    /**
     * 源表count总条数
     */
    @ApiModelProperty(value="源表count总条数")
    private Long sourceCountNumber;
    /**
     * 目标表count总条数
     */
    @ApiModelProperty(value="目标表count总条数")
    private Long targetCountNumber;
    /**
     * 创建人
     */
    @ApiModelProperty(value="创建人")
    @TableField(fill = FieldFill.INSERT)
    private String createBy;
    /**
     * 创建时间
     */
    @ApiModelProperty(value="创建时间")
    @TableField(fill = FieldFill.INSERT)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss" ,timezone = "GMT+8")
    private Date createTime;
    /**
     * 更新人
     */
    @ApiModelProperty(value="更新人")
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private String lastUpdateBy;
    /**
     * 更新时间
     */
    @ApiModelProperty(value="更新时间")
    @TableField(fill = FieldFill.INSERT_UPDATE)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss" ,timezone = "GMT+8")
    private Date lastUpdateTime;
    /**
     * 删除标识
     */
    @ApiModelProperty(value="删除标识")
    @TableField(fill = FieldFill.INSERT)
    private String del;


    /**
     * 源分区值（多个值逗号隔开）
     */
    @TableField(exist = false)
    private String sourceValue;
    /**
     * 源分区
     */
    @TableField(exist = false)
    private String sourcePartition;
    /**
     * 日期分区
     */
    @TableField(exist = false)
    private String datePartition;
    /**
     * 保留数据（月）
     */
    @TableField(exist = false)
    private String holdDataMonth;
    /**
     * 日志插入对象
     */
    @TableField(exist = false)
    private FileAppender heatLog;

    /**
     * 表任务初始化数据，保存数据库
     */
    public void initializeData(String taskDate, String sourceName, String dbName,
                            String tbName,String dbTbName,String taskState,String logPath,
                            String syncRangeStartTime, String syncRangeEndTime){
        this.taskDate = taskDate;
        this.sourceName = sourceName;
        this.dbName = dbName;
        this.tbName = tbName;
        this.dbTbName = dbTbName;
        this.taskState = taskState;
        this.logPath = logPath;
        this.syncRangeStartTime = syncRangeStartTime;
        this.syncRangeEndTime = syncRangeEndTime;
    }

    /**
     * 分区信息、保留月
     */
    public void partitionData(String sourceValue,String sourcePartition, String datePartition, String holdDataMonth){
        this.sourceValue = sourceValue;
        this.sourcePartition = sourcePartition;
        this.datePartition = datePartition;
        this.holdDataMonth = holdDataMonth;
    }

    /**
     * 获取增量ls完整Shell命令
     */
    public Map<String,String> gainLsShell(String JQHdfsPrefix, String rangeAllDate, String sourceFilePath, String grepHdfsFiles){
        String dbHdfsPath = JQHdfsPrefix + this.dbName +".db/"+ this.tbName;
        String filePath = sourceFilePath + this.sourceName + "_" + this.dbName + "_" + this.tbName + ".txt";
        //例子(sh /opt/script/grep_hdfs_files.sh hdfs://172.21.32.16:8020/user/hive/warehouse/ods.db/table_1 2024-01-01|2024-01-02|2024-01-03 /opt/dmpro/sourceFile/heatBackup/源_库_表.txt)
        String grepLsShell = grepHdfsFiles +" "+ dbHdfsPath +" "+rangeAllDate+" "+filePath;
        Map<String, String> map = new HashMap<>();
        map.put("grepLsShell",grepLsShell);
        map.put("filePath",filePath);
        return map;
    }
}
